/*
 * CollectionDao.java
 *
 * Created on 4 de abril de 2006, 22:28
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import fmap.DatosCompartidos;
import fmap.model.GroupFile;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import fmap.utils.IConstants;

/**
 *
 * @author javi
 */
public class GroupFileDao
{
    
    /** Creates a new instance of CollectionDao */
    public GroupFileDao ()
    {
    }
    
    
    public Vector selectGroupFile ()
    {
        Vector vGroupFile = new Vector ();
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idGroupFile, name, extensions FROM fmap.groupFile ");
            
            ResultSet rs = ps.executeQuery ();
            
            while( rs.next () )
            {
                vGroupFile.add ( new GroupFile (rs.getInt (1), rs.getString (2), rs.getString (3)) );
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return vGroupFile;
    }
    
    
    public GroupFile selectGroupFile ( int idGroupFile )
    {
        GroupFile groupFile = null;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT idGroupFile, name, extensions FROM fmap.groupFile WHERE idGroupFile=?");
            
            ps.setInt (1, idGroupFile);
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                groupFile = new GroupFile (rs.getInt (1), rs.getString (2), rs.getString (3));
            }
            
            rs.close ();
            ps.close ();
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
        }
        
        return groupFile;
    }
    
    
    public boolean insertGroupFile ( String name, String extensions )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "INSERT INTO fmap.groupFile ( name, extensions ) VALUES(?,?)");
            
            ps.setString (1, name);
            ps.setString (2, extensions);
            
            ps.executeUpdate ();
            
            ps.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
            return false;
        }
        
        return true;
    }
    
    
    public boolean updateGroupFile ( int idGroupFile, String name, String extensions )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "UPDATE fmap.groupFile SET name =?, extensions=? WHERE idGroupFile=?");
            
            ps.setString (1, name);
            ps.setString (2, extensions);
            ps.setInt (3,idGroupFile);
            
            ps.executeUpdate ();
            
            ps.close ();
            
        }
        catch( Exception e)
        {
            System.out.println ( e.getMessage () );
            return false;
        }
        
        return true;
    }
    
    
    public boolean deleteGroupFile ( int idGroupFile )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.groupFile WHERE idGroupFile=?");
            ps.setInt ( 1, idGroupFile );
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
}